import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * model 任务执行器
 * @date: 2018-9-3


 */
import { getResponse, createPagination } from 'utils/utils';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
import { fetchGroupsList as _fetchGroupsList, createGroups as _createGroups, updateGroups as _updateGroups, deleteGroups as _deleteGroups, fetchExecutorList as _fetchExecutorList, deleteExecutor as _deleteExecutor, updateExecutor as _updateExecutor } from "../services/jobGroupService";
export default {
  namespace: 'jobGroup',
  state: {
    groupsList: [],
    // 执行器列表
    jobsList: [],
    // 执行器任务列表
    pagination: {},
    // 分页器
    modalVisible: false,
    // 控制模态框显示
    statusList: [],
    executorList: [] // 执行器配置列表

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, statusList;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CSDR.EXECUTOR_STATUS');

            case 4:
              _context.t1 = _context.sent;
              statusList = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  statusList: statusList
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    fetchGroupsList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchGroupsList(_ref2, _ref3) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchGroupsList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.t0 = getResponse;
              _context2.next = 5;
              return call(_fetchGroupsList, payload);

            case 5:
              _context2.t1 = _context2.sent;
              result = (0, _context2.t0)(_context2.t1);

              if (!result) {
                _context2.next = 10;
                break;
              }

              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  groupsList: result.content,
                  pagination: createPagination(result)
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchGroupsList);
    }),
    createGroups: /*#__PURE__*/_regeneratorRuntime.mark(function createGroups(_ref4, _ref5) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function createGroups$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.t0 = getResponse;
              _context3.next = 5;
              return call(_createGroups, _objectSpread({}, payload));

            case 5:
              _context3.t1 = _context3.sent;
              result = (0, _context3.t0)(_context3.t1);
              return _context3.abrupt("return", result);

            case 8:
            case "end":
              return _context3.stop();
          }
        }
      }, createGroups);
    }),
    updateGroups: /*#__PURE__*/_regeneratorRuntime.mark(function updateGroups(_ref6, _ref7) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateGroups$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.t0 = getResponse;
              _context4.next = 5;
              return call(_updateGroups, _objectSpread({}, payload));

            case 5:
              _context4.t1 = _context4.sent;
              result = (0, _context4.t0)(_context4.t1);
              return _context4.abrupt("return", result);

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, updateGroups);
    }),
    deleteGroups: /*#__PURE__*/_regeneratorRuntime.mark(function deleteGroups(_ref8, _ref9) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteGroups$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.t0 = getResponse;
              _context5.next = 5;
              return call(_deleteGroups, _objectSpread({}, payload));

            case 5:
              _context5.t1 = _context5.sent;
              result = (0, _context5.t0)(_context5.t1);
              return _context5.abrupt("return", result);

            case 8:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteGroups);
    }),
    fetchExecutorList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchExecutorList(_ref10, _ref11) {
      var payload, call, put, result;
      return _regeneratorRuntime.wrap(function fetchExecutorList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call, put = _ref11.put;
              _context6.t0 = getResponse;
              _context6.next = 5;
              return call(_fetchExecutorList, payload);

            case 5:
              _context6.t1 = _context6.sent;
              result = (0, _context6.t0)(_context6.t1);

              if (!result) {
                _context6.next = 10;
                break;
              }

              _context6.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  executorList: result.map(function (item) {
                    return _objectSpread(_objectSpread({}, item), {}, {
                      uuid: "".concat(item.executorId, "-").concat(item.address)
                    });
                  })
                }
              });

            case 10:
            case "end":
              return _context6.stop();
          }
        }
      }, fetchExecutorList);
    }),
    updateExecutor: /*#__PURE__*/_regeneratorRuntime.mark(function updateExecutor(_ref12, _ref13) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function updateExecutor$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.t0 = getResponse;
              _context7.next = 5;
              return call(_updateExecutor, payload);

            case 5:
              _context7.t1 = _context7.sent;
              result = (0, _context7.t0)(_context7.t1);
              return _context7.abrupt("return", result);

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, updateExecutor);
    }),
    deleteExecutor: /*#__PURE__*/_regeneratorRuntime.mark(function deleteExecutor(_ref14, _ref15) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteExecutor$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.t0 = getResponse;
              _context8.next = 5;
              return call(_deleteExecutor, payload);

            case 5:
              _context8.t1 = _context8.sent;
              result = (0, _context8.t0)(_context8.t1);
              return _context8.abrupt("return", result);

            case 8:
            case "end":
              return _context8.stop();
          }
        }
      }, deleteExecutor);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref16) {
      var payload = _ref16.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};